package com.iflysse.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
@Aspect
public class LoggerAspect {
	
	//创建日志对象
	private Logger logger = LoggerFactory.getLogger(LoggerAspect.class);
	
	//定义一个切面
	@Pointcut(value = "execution(* com.iflysse.controller.*.*(..)) "
			+ "&& !execution(* com.iflysse.controller.LoginController.*(..)) "
			+ "&& !execution(* com.iflysse.controller.UserInfoController.updateUserHeadImage(..))")
	public void pointCut() {
		
	}
		
	@Around("pointCut()")
	public Object countTime(ProceedingJoinPoint joinPoint) throws Throwable{
		String className = joinPoint.getClass().toString();//获取类 class
		String methodName = joinPoint.getSignature().getName();//获取方法名
		Object objs = joinPoint.getArgs();//获取传入参数
		ObjectMapper mapper = new ObjectMapper();//用来展示参数或者结果
		logger.info("调用前"+className+":"+methodName+"传入参数："+mapper.writeValueAsString(objs));
		Object obj = joinPoint.proceed();// 运行方法
		logger.info("调用后"+className+":"+methodName+"返回值："+mapper.writeValueAsString(obj));
        return obj;
	}
}
